<?php

namespace addons\lfoss;

use OSS\Core\OssException;
use OSS\OssClient;
use think\Addons;
use think\exception\HttpResponseException;
use think\File;
use think\Response;

/**
 * 插件
 */
class Lfoss extends Addons
{

    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {

        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {

        return true;
    }

    /**
     * 插件启用方法
     * @return bool
     */
    public function enable()
    {

        return true;
    }

    /**
     * 插件禁用方法
     * @return bool
     */
    public function disable()
    {

        return true;
    }

    /**
     * 实现钩子方法
     * @return mixed
     */
    public function uploadAfter($param)
    {
        $config = $this->getConfig();
        if (!$config['is_open']) {
            return;
        }

        $OssPath = "https://gzns.oss-cn-shenzhen.aliyuncs.com/";
        $accessKeyId = "LTAI9HrmZP59xIU4";
        $accessKeySecret = "0BCM2fQBbS1cjVRJuTAiDaltGiwXDB";
//        $endpoint = "http://oss-cn-shenzhen-internal.aliyuncs.com";   //内网
        $endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
        // 存储空间名称
        $bucket = "gzns";
        try {
            $url = $param->getData('url');
            $file = new File(ROOT_PATH . 'public' . $url);
            $fileName = $file->getFilename();

            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
//            $ossClient->createBucket($bucket);
            $result = $ossClient->uploadFile($bucket, $fileName, $file->getPathname());
            if ($result && $result['info'] && $result['info']['http_code'] == 200) {
                $param->update([
                    "id" => $param->getData('id'),
                    "url" => $OssPath . $fileName,
                    "storage" => "oss",
                ]);
//                header("HTTP/1.0 200 OK");
//                echo json_encode([
//                    "code" => 1,
//                    "data" => [
//                        "url" => $OssPath . $fileName,
//                    ],
//                    "msg" => "上传成功1",
//                ]);
//                exit(0);
                $result = [
                    'code' => 1,
                    'msg'  => "上传成功12",
                    "data" => [
                        "url" => $OssPath . $fileName,
                    ],
                ];
                $type     = 'json';
                $response = Response::create($result, $type)->header([]);

                throw new HttpResponseException($response);
            }
        } catch (OssException $e) {
//            print $e->getMessage();
            $result = [
                'code' => 0,
                'msg'  => "上传失败",
                "data" => null,
            ];
            $type     = 'json';
            $response = Response::create($result, $type)->header([]);

            throw new HttpResponseException($response);
//            header("HTTP/1.0 200 OK");
//            echo json_encode([
//                "code" => 0,
//                "url" => null,
//                "msg" => "上传失败",
//            ]);
//            exit(0);
        }
    }

}
